﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace NowFloats.RichSnippetExtractor.RDFa
{
    public class RdfaRequest
    {
        public float? GetRating(String Url)
        {
            HtmlDocument document = (new HtmlWeb()).Load(Url);
            return GetRating(document);
        }

        public float? GetRating(HtmlDocument document)
        {
            foreach (var node in document.DocumentNode.Descendants())
            {
                if (node.Attributes.Contains("typeof"))
                {
                    if (String.Compare(node.Attributes["typeof"].Value, "v:Review", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        foreach (var childNode in node.Descendants())
                        {
                            if (childNode.Attributes.Contains("property"))
                            {
                                if (String.Compare(childNode.Attributes["property"].Value, "v:rating", StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    float rating;
                                    if (float.TryParse(childNode.InnerText, out rating))
                                        return rating;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }

        public float? GetAverageRDFaRatings(String Url)
        {
            HtmlDocument document = (new HtmlWeb()).Load(Url);
            return GetAverageRDFaRatings(document);
        }

        public float? GetAverageRDFaRatings(HtmlDocument document)
        {
            foreach (var node in document.DocumentNode.Descendants())
            {
                if (node.Attributes.Contains("typeof"))
                {
                    if (String.Compare(node.Attributes["typeof"].Value, "v:Review-aggregate", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        foreach (var childNode in node.Descendants())
                        {
                            if (childNode.Attributes.Contains("property"))
                            {
                                if (String.Compare(childNode.Attributes["property"].Value, "v:average", StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    float rating;
                                    if (childNode.Attributes.Contains("content"))
                                    {
                                        if (float.TryParse(childNode.Attributes["content"].Value, out rating))
                                            return rating;
                                    }
                                    if (float.TryParse(childNode.InnerText, out rating))
                                        return rating;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
    }
}
